import { _ } from "../util";
import store from "../store";
import router from "../router";

interface userModel {
    account: string;
    authentication: string;

}
class user {
    account: string;
    constructor() {
        this.account = '';
    }
    // 登录
    login(_data: userModel) {
        this.account = _data.account;
        _.setLocalStorage("USER", "CURRENT_USER", {
            account: this.account,
        });

        _.setLocalStorage("USER", this.account, _data, 1440);
        store.commit("SET_USER_DATA", _data);
        router.replace("/");
    }

    // 注销
    cancellation() {
        _.delLocalStorage("USER", this.account);
        _.delLocalStorage("USER", "CURRENT_USER");
        router.replace("/login");
    }

    // 判断是否登录，并储存信息
    isAuthentication(): boolean {
        /**
        * 当前登录帐户
        */
        let CURRENT_USER = _.getLocalStorage("USER", "CURRENT_USER");
        if (_.isEmpty(CURRENT_USER)) {
            return false;
        }
        /**
         * 当前登录帐户去取用户信息
         */
        let USER_DATA = _.getLocalStorage("USER", CURRENT_USER.account);
        if (_.isEmpty(USER_DATA)) {
            return false;
        }
        /**
         * 向store设置用户信息
         */
        this.account = CURRENT_USER.account;
        store.commit("SET_USER_DATA", USER_DATA);
        return true;
    }
}


export default new user();